#!/usr/bin/env python3
# coding: utf-8

"""
2024-08-08 F 运行在pi zero上的, 服务器管理工具
"""
import sys
from pathlib import Path
import time
import threading
from gpiozero import LED, Buzzer, Button
from signal import pause
import datetime
import subprocess
import random
## sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from servermanagerutil import *
from servermanager import *

## IO口定义
# SERVER6
LED_S6_RED = LED(15)
LED_S6_GREEN = LED(18)
# SERVER7
LED_S7_RED = LED(4)
LED_S7_GREEN = LED(17)
# 演示
LED_DEMO_RED = LED(27)
LED_DEMO_GREEN = LED(22)
# 实施
LED_TEST_RED = LED(10)
LED_TEST_GREEN = LED(9)
# SERVER9
LED_S9_RED = LED(11)
LED_S9_GREEN = LED(5)
# 蜂鸣器
BEEP = Buzzer(26)
# 主板电源
LED_POWER_RED = LED(21)
LED_POWER_GREEN = LED(20)
BUTTON_POWER = Button(16)

# 开关: 手动控制
BUTTON_MANUAL_START = Button(8)
BUTTON_MANUAL_STOP = Button(7)
# 开关: 自动打开
BUTTON_AUTOON = Button(25)

## 开关: 光敏开关
BUTTON_LIGHT = Button(14, pull_up=False)       ## , pull_up=False
## 持续多久没有光的时候才会触发


OPERATING = False
REFRESHING = True

LEDS = [LED_S6_RED, LED_S6_GREEN, LED_S7_RED, LED_S7_GREEN, LED_DEMO_RED, LED_DEMO_GREEN, LED_TEST_RED, LED_TEST_GREEN, LED_S9_RED, LED_S9_GREEN, LED_POWER_RED, LED_POWER_GREEN]



def current_timestamp() :
    """
        返回当前时间戳的字符串, 带3位毫秒
    """
    return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]

def init_LED() :
    ## 关闭所有IO口
    ## LED全部都是作为IO口模拟地线,因此是全部打开(高阻态)
    LED_S6_RED.off()
    LED_S6_GREEN.off()
    LED_S7_RED.off()
    LED_S7_GREEN.off()
    LED_DEMO_RED.off()
    LED_DEMO_GREEN.off()
    LED_TEST_RED.off()
    LED_TEST_GREEN.off()
    LED_S9_RED.off()
    LED_S9_GREEN.off()
    LED_POWER_RED.off()
    LED_POWER_GREEN.off()


def init() :
    ## 关闭所有IO口
    ## LED全部都是作为IO口模拟地线,因此是全部打开(高阻态)
    init_LED()
    # BUTTON_AUTOON.close()
    # BUTTON_MANUAL_START.close()
    # BUTTON_MANUAL_STOP.close()





def test() :
    BEEP.off()
    time.sleep(0.5)
    ##BEEP.on()
    print("RED ON")
    LED_S6_RED.on()
    ## LED_POWER_RED.blink(on_time=0.5, off_time=0.5, n=3)
    LED_S7_RED.on()
    LED_DEMO_RED.on()
    LED_TEST_RED.on()
    LED_S9_RED.on()
    LED_POWER_RED.on()
    time.sleep(10)
    print("RED OFF")
    LED_S6_RED.off()
    LED_S7_RED.off()
    LED_DEMO_RED.off()
    LED_TEST_RED.off()
    LED_S9_RED.off()
    LED_POWER_RED.off()
    #LED_POWER_GREEN.off()
    time.sleep(2)
    BEEP.off()
    time.sleep(0.1)
    ##BEEP.on()
    time.sleep(0.1)
    BEEP.off()
    time.sleep(0.1)
    ##BEEP.on()
    print("GREEN ON")
    LED_S6_GREEN.on()
    LED_S7_GREEN.on()
    LED_DEMO_GREEN.on()
    LED_TEST_GREEN.on()
    LED_S9_GREEN.on()
    LED_POWER_GREEN.on()
    time.sleep(10)
    print("GREEN OFF")
    LED_S6_GREEN.off()
    LED_S7_GREEN.off()
    LED_DEMO_GREEN.off()
    LED_TEST_GREEN.off()
    LED_S9_GREEN.off()
    LED_POWER_GREEN.off()
    time.sleep(10)
init()



while True :
    # check_server_state()
    test()
    # time.sleep(10)
    ## start_check_server_state()
    time.sleep(0.01)